package com.github.tvbox.osc.ui.adapter;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import io.github.source.media.bean.MediaCategory;

import com.github.tvbox.osc.ui.activity.UserFragmentKt;
import com.github.tvbox.osc.ui.fragment.GridFragmentKt;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @user acer
 * @date 2018/12/4
 */

public class HomePageAdapter extends FragmentStateAdapter {
    public FragmentManager fragmentManager;
    public final List<MediaCategory> data = new ArrayList<>();
    // 使用 ConcurrentHashMap 线程安全
    private final Map<Integer, WeakReference<Fragment>> fragmentCache = new ConcurrentHashMap<>();// 用于缓存Fragment实例

    public HomePageAdapter(@NonNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    public HomePageAdapter(@NonNull Fragment fragment) {
        super(fragment);
    }

    public HomePageAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }


    /**
     * 设置数据（刷新）
     *
     * @param list list
     */
    public void setData(List<MediaCategory> list) {
        clear();
        this.data.addAll(list != null ? list : new ArrayList<>());
        Log.d("HomePageAdapter", "setData= " + data.size());
    }

    public void clear() {
        if (data != null) {
            data.clear();
        }
        clearFragmentCache();
    }

    /**
     * 在适配器中定义清除缓存的方法
     */
    public void clearFragmentCache() {
        fragmentCache.clear();
    }

    /**
     * 获取指定位置的Fragment。(如果没有会创建一个）
     *
     * @param position 页面位置
     * @return 指定位置的Fragment
     */
    public Fragment getFragment(int position) {
        // 从缓存中获取Fragment
        WeakReference<Fragment> ref = fragmentCache.get(position);
        Fragment fragment = ref != null ? ref.get() : null;
        if (fragment == null) {
            // 如果没有缓存，则创建一个新的Fragment并放入缓存
            fragment = createFragment(position);
        }
        return fragment;
    }

    //================================
    @Override
    public int getItemCount() {
        int size = data != null ? data.size() : 0;
        return size;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        MediaCategory item = this.data.get(position);
        // 先从缓存中获取Fragment

        WeakReference<Fragment> ref = fragmentCache.get(position);
        Fragment fragment = ref != null ? ref.get() : null;
        if (fragment == null) {
            if (position == 0 || item != null && item.id.equals("my0")) {
                fragment = UserFragmentKt.newInstance(item); // 这里假设null参数是合理的
            } else {
                fragment = GridFragmentKt.newInstance(item);
            }

            // 使用弱引用避免内存泄漏// 将新创建的Fragment放入缓存
            fragmentCache.put(position, new WeakReference<>(fragment));
        }
        return fragment;

    }

}
